package com.lanou.bookstore.user.service;

import com.lanou.bookstore.user.dao.UserDao;
import com.lanou.bookstore.user.domain.User;

import java.sql.SQLException;

public class UserService {
    private UserDao userDao = new UserDao();

    public boolean regist(User form) {
        User user = null;
        try {
            user = userDao.findByUsername(form.getUsername());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user == null) {
            User user1 = null;
            try {
                user1 = userDao.findByEmail(form.getEmail());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (user1 == null) {
                try {
                    userDao.add(form);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean login(User form) {
        User user = null;
        try {
            user = userDao.findByUsername(form.getUsername());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user != null && form.getPassword().equals(user.getPassword())) {
            return true;
        } else {
            return false;
        }
    }

    public User findAllByUsername(String username) {
        User user = null;
        try {
            user = userDao.findByUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
